/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package br.com.jteam.jfcm.gui.panel;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import br.com.jteam.jfcm.controller.Subscriber;
import br.com.jteam.jfcm.controller.event.Event;
import br.com.jteam.jfcm.controller.event.OpenFileEvent;
import br.com.jteam.jfcm.controller.event.OpenHistoryFileEvent;
import br.com.jteam.jfcm.controller.event.ShowImageEvent;
import br.com.jteam.jfcm.controller.event.ShowTextEvent;

/**
 * Classe que guarda o conte&uacute;do principal da aplica&ccedil;&atilde;o
 * &eacute; notificada da ocorr&ecirc;ncia de {@link ShowImageEvent},
 * {@link ShowTextEvent}, {@link OpenFileEvent} e {@link OpenHistoryFileEvent}.
 * 
 * @see br.com.jteam.jfcm.controller.event.ShowImageEvent
 * @see br.com.jteam.jfcm.controller.event.ShowTextEvent
 * @see br.com.jteam.jfcm.controller.event.OpenFileEvent
 * @see br.com.jteam.jfcm.controller.event.OpenHistoryFileEvent
 */
public class ContentPanel extends JPanel implements Subscriber
{
	/**
	 * Pain&eacute;l que exibe imagem.
	 */
	private ImageViewerPanel imageViewerPanel;

	/**
	 * Pain&eacute;l que exibe texto.
	 */
	private TextAreaViewerPanel textAreaViewerPanel;
	{
		setLayout(new BorderLayout());
		setContent(new JPanel());
		imageViewerPanel = new ImageViewerPanel();
		textAreaViewerPanel = new TextAreaViewerPanel();
	}

	public void notify(Event event)
	{
		if (event.equals(OpenFileEvent.class)
						|| event.getClass().equals(OpenHistoryFileEvent.class))
		{
			setContent(new JPanel());
		} else if (event.getClass().equals(ShowImageEvent.class))
		{
			ShowImageEvent ev = (ShowImageEvent) event;
			imageViewerPanel.setImage(ev.getImage());
			setContent(imageViewerPanel);
		} else if (event.getClass().equals(ShowTextEvent.class))
		{
			ShowTextEvent ev = (ShowTextEvent) event;
			textAreaViewerPanel.setText(ev.getContent());
			setContent(textAreaViewerPanel);
		}
	}

	/**
	 * Atualiza o conte&uacute;do exibido no container.
	 * 
	 * @param component
	 *            Pain&eacute;l com o conte&uacute;do a ser inserido no
	 *            container.
	 */
	private void setContent(JPanel component)
	{
		removeAll();
		JScrollPane scrollPane = new JScrollPane(component);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(scrollPane, BorderLayout.CENTER);
		SwingUtilities.updateComponentTreeUI(this);
	}
}
